package com.wanmait.community.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.aop.OperationLogInfo;
import com.wanmait.community.entity.Device;
import com.wanmait.community.entity.InboundPic;
import com.wanmait.community.service.DeviceService;
import com.wanmait.community.util.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("device")
public class DeviceController {
    @Resource
    private DeviceService deviceService;
    @PostMapping("list")
    public AjaxResult list(@RequestBody Device device,Integer pageNum) {//form提交不用加@RequestBody
        Integer pageSize = 6;//每页几条，因为没有config表，这里先写死
        PageInfo<Device> list = deviceService.findAll(device,pageNum,pageSize);
        return AjaxResult.success().put("list", list);
    }
    @GetMapping("getStandard")
    public AjaxResult getStandard(@RequestParam Integer id) {//查单个device
        Device device = deviceService.getKeepStandardById(id);
        return AjaxResult.success().put("device", device);
    }
    @PostMapping("insertDevice")
    @OperationLogInfo("添加了一台设备")//添加操作日志的注解，查不用加日志
    public AjaxResult insertDevice(@RequestBody Device device) {
        //处理devicePic路径，如果为空设置为默认的暂无图片即default.jpg
        if (device.getInboundPic() == null) {
            device.setInboundPic(new InboundPic()); // 如果inboundPic为null，创建新对象
        }
        if (device.getInboundPic().getDevicePic() == null || device.getInboundPic().getDevicePic().isEmpty()) {
            device.getInboundPic().setDevicePic("default.jpg");
        }
        deviceService.insertDeviceInfo(device);
        return AjaxResult.success("添加成功");
    }
   @PostMapping("getDeviceByUpdate")//根据id查将要修改的原始数据,包含图片
    public AjaxResult getDeviceByUpdate(@RequestBody Map<String, Integer> request) {
       Integer id = request.get("id");
       Device updateFrom = deviceService.getKeepStandardById(id);
       return AjaxResult.success().put("updateFrom", updateFrom);
   }
   @PostMapping("updateDevice")
   @OperationLogInfo("修改了一台设备")
    public AjaxResult updateDevice(@RequestBody Device device) throws JsonProcessingException {
        deviceService.updateDeviceInfo(device);
        return AjaxResult.success("修改成功");
    }
    @PostMapping("delete")
    @OperationLogInfo("删除了一台设备")// 改为DeleteMapping
    public AjaxResult delete(@RequestBody Device device) throws JsonProcessingException {
        deviceService.deleteDeviceInfo(device);
        return AjaxResult.success("删除了一个设备"); // 确保返回的AjaxResult包含msg字段
    }
//    add图片上传
    @PostMapping("uploadImages")
    @OperationLogInfo("添加上传设备图片一张")
    public AjaxResult uploadImages(@RequestParam("file") MultipartFile file) {
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadImages/";//文件存储物理路径，在编译时把文件复制到tag
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());//唯一文件名
            String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
            file.transferTo(new File(path+File.separator+fileName));//保存文件
            return AjaxResult.success("上传成功").put("fileName",fileName);
        } catch (IOException e) {
            return AjaxResult.error("上传失败");
        }
    }
//    update图片上传
    @PostMapping("updateUploadImages")
    @OperationLogInfo("修改上传设备图片一张")
    public AjaxResult updateUploadImages(@RequestParam("file") MultipartFile file) {
        try {
            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadImages/";//文件存储物理路径，在编译时把文件复制到tag
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());//唯一文件名
            String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
            file.transferTo(new File(path+File.separator+fileName));//保存文件
            return AjaxResult.success("上传成功").put("fileName",fileName);
        } catch (IOException e) {
            return AjaxResult.error("上传失败");
        }
    }

}
